package okhttp.itydl.com.howardokhttpframe;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.io.File;

import okhttp.itydl.com.download.DownloadManager;
import okhttp.itydl.com.download.utils.Logger;
import okhttp.itydl.com.download.http.DownloadCallback;
import okhttp.itydl.com.download.scheduler.IScheduler;
import okhttp.itydl.com.download.scheduler.UiScheduler;

/**
 * @author howard
 */
public class MainActivity extends AppCompatActivity {

    private IScheduler UiScheduler = new UiScheduler();
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = findViewById(R.id.iv_image);
//        HttpManager.getInstance().asyncRequest("https://img.mukewang.com/5733fcce0001103506000338-240-135.jpg", new DownloadCallback() {
//            @Override
//            public void success(File file) {
//                Logger.debug("yangdaolong",file.getAbsolutePath());
//                final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//
//                UiScheduler.execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        mImageView.setImageBitmap(bitmap);
//                    }
//                });
//            }
//
//            @Override
//            public void fail(int errorCode, String errorMessage) {
//                Logger.debug("yangdaolong","errorCode:"+errorCode +",errorMessage"+errorMessage);
//            }
//
//            @Override
//            public void progress(int progress) {
//
//            }
//        });

        DownloadManager.getInstance().download("https://img.mukewang.com/5733fcce0001103506000338-240-135.jpg", new DownloadCallback() {
            @Override
            public void success(File file) {
                Logger.debug("yangdaolong", file.getAbsolutePath());
                final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                UiScheduler.execute(new Runnable() {
                    @Override
                    public void run() {
                        mImageView.setImageBitmap(bitmap);
                    }
                });
            }

            @Override
            public void fail(int errorCode, String errorMessage) {
                Logger.debug("yangdaolong","errorCode:"+errorCode +",errorMessage"+errorMessage);
            }

            @Override
            public void progress(int progress) {

            }
        });
    }
}
